// JavaScript Document
$(function () {
	$("#aFilm").addClass("clickOn")
	$("#menu a").on("click",function(){
		window.location.replace("show_main")
	})
	$("#cinemaMenuSelect a").on("click",function(){
		$(this).siblings("a").removeClass("cinemaSelected")
		$(this).addClass("cinemaSelected")
	})	
	$("#timeSelect a").on("click",function(){
		$(this).siblings("a").removeClass("cinemaSelected")
		$(this).addClass("cinemaSelected")
	})
	$("#cinemaMenuSelect a").on("click",function(){
		$(this).siblings("a").removeClass("cinemaSelected")
		$(this).addClass("cinemaSelected")
		var Film_name=$(this).html()
		$.ajax({
			type:"post",
			data:{Film_name:Film_name},
			url:"http://127.0.0.1:8080/findFilmInfo_cinema",
			success:function(data){
			//	alert("成功");
				var str=""
				$("#filmInfo1").html(str)

					str="<div id=\"hintText\">\n" +
						"<img src='"+data.data.Url+"'/>\n" +
						"<a><h4>"+data.data.Film_name+"</h4>&nbsp;"+data.data.Score+"</a>\n" +
						"<hr>\n" +
						"<p>\n" +
						"导演："+data.data.Director+"<br>\n" +
						"主演："+data.data.Actor+"<br>\n" +
						"类型："+data.data.Film_type+"<br>\n" +
						"制片国家/地区："+data.data.Make_film_area+"<br>\n" +
						"</p>\n" +
						"</div>"
					$("#filmInfo1").append(str)

			},

			error:function () {
				alert("错误");
			}
		});
		var cinema=$("#filmTitle").html()
		$.ajax({
			type:"post",
			data:{cinema:cinema,filmTitle:Film_name},
			url:"http://127.0.0.1:8080/FindAFilmTimeByCinema",
			success:function(data){
				//alert("成功"+data.data.length);

				var str=""
				$("#timeSelect").html(str)

				for(var i=0;i<data.data.length;i++) {
					str = "<a>"+data.data[i]+"</a>"
					$("#timeSelect").append(str)
				}
			},

			error:function () {
				alert("错误");
			}
		});
	})
	$(document).on("click","#timeSelect a",function(){

		$(this).siblings("a").removeClass("cinemaSelected")
		$(this).addClass("cinemaSelected")
		var filmTitle=$("#cinemaMenuSelect .cinemaSelected").text();
		var date=$(this).text()
		//alert(date)
		$.ajax({
			type:"get",
			data:{date:date,filmTitle:filmTitle},
			url:"http://127.0.0.1:8080/FindAFilmTimeByDate",
			success:function(data){
			//	alert("成功"+data.data.length);


				// alert($("#cinemaSelect").children("select").html())
				var str=""
				$("#DataTableInput").html(str)
				for(var i=0;i<data.data.length;i++){
					var time=new String();
					time=data.data[i].Film_time.toString();
					var timeArr=new Array();
					timeArr=time.split('-');
					//alert(time)
					str= '<tr><td class="timesId"  style="display: none">'+data.data[i].Film_times_id+'</td>'
						+'<td><h4>'+timeArr[0]+'</h4><p>预计'+timeArr[1]+'散场</p></td>'
						+'<td><p>'+data.data[i].Language_version+'</p></td>'
						+'<td><p>'+data.data[i].Hall_name+'</p></td>'

					var seat=new String();
					seat=data.data[i].Seat_info.toString();
					var seatArr=new Array();
					seatArr=seat.split('');
					var seatCount=0;
					var seatStr="宽松"
					for(var j=0;j<seatArr.length;j++)
					{
						if(seatArr[j]==1){
							seatCount++
						}
					}
					//alert(seatCount)
					if(seatCount>=0&&seatCount<=48){
						seatStr="宽松"
						str+='<td><p>'+seatStr+'</p></td>'
					}else {
						seatStr="紧张"
						str+='<td><p style="color: #EB002A">'+seatStr+'</p></td>'
					}

					str+='<td><h4>'+data.data[i].Film_price+'.00</h4><s>'+data.data[i].Film_price+'.00</s></td>'
						+'<td>'
						+'<div class="hotCinemaBtn">'
						+'<a>选座购票</a>'
						+'</div>'
						+'</td>'
						+'</tr>'
					//str=data.msg
					$("#DataTableInput").append(str);
				}
			},

			error:function () {
				alert("错误");
			}
		});
	})
	$(document).on("click",".hotCinemaBtn",function () {
		var time_id= $(this).parents("td").siblings(".timesId").html()
		//alert(time_id)
		formpost("http://127.0.0.1:8080/show_login",{'time_id' :time_id});
	})
	function formpost(url, postData) {
		var tempform = document.createElement("form");
		tempform.action = url;
		tempform.target = "_self";
		tempform.method = "post";
		tempform.style.display = "none";
		for (var x in postData) {
			var opt = document.createElement("textarea");
			opt.name = x;
			opt.value = postData[x];
			tempform.appendChild(opt);
		}
		document.body.appendChild(tempform);
		tempform.submit();
	}
})